package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/department")
@RestController//@RestController 它是一个组合注解        @RestController =@Controller+@ResponseBody
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    /**
     * 保存/修改部门
     *
     * eclipse快速生成try catch   ctrl+alt+t
     * @param department
     * @return
     */
    @PutMapping// @PutMapping = @RequestMapping(method = RequestMethod.PUT)
    public AjaxResult addOrUpdate(@RequestBody Department department){  //前后端分离的项目，前端传递的数据都是json对象，所以后台想要接受对应的数据，必须要加@RequestBody注解，否则接收不了
        try {
            departmentService.addOrUpdate(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }




    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
    /**
     * 批量删除数据
     * @param departments 部门集合
     * @return
     */
    @PatchMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Department> departments){
        try {
            departmentService.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


    /**
     * 根据id查询对象
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Department loadById(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }


    /**
     * 查询所有的部门对象
     * @return
     */
    @PatchMapping
    public PageList<Department> loadPageByQuery(@RequestBody DepartmentQuery query){
        return departmentService.loadPageByQuery(query);
    }


    /**
     * 查询所有的部门列表
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }


}
